package com.emip.servlet.site;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.emip.bean.AlarmConfigBean;
import com.emip.bean.ItemBean;
import com.emip.bean.SiteBean;
import com.emip.bean.SiteItemBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.AlarmConfigService;
import com.emip.service.ItemService;
import com.emip.service.SiteItemService;
import com.emip.service.SiteService;
import com.emip.util.TableFormat;

@Controller
@RequestMapping(value="/site/item")
public class SiteItemAction {

	@Autowired
	SiteItemService siteItemService;
	
	@Autowired
	SiteService siteService;
	
	@Autowired
	ItemService itemService;
	
	@Autowired
	AlarmConfigService alarmConfigService;
	
	@RequestMapping(value="/management")
	public String index()
	{		
		return "/site/SiteItemManagement";
	}
	
	@RequestMapping(value="/getItemBySite")
	//public @ResponseBody Table<SiteItemBean> getItemBySite() {
	public @ResponseBody Table<SiteItemBean> getItemBySite(@RequestParam("siteID") String siteID) {
		List<SiteItemBean> result = siteItemService.getSiteItemBySiteID(siteID);
		//List<SiteItemBean> result = siteItemService.getSiteItemBySiteID("SH3");
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<SiteItemBean>().formTable(totalPages, 1, records, result);
	}
	

	@RequestMapping(value="/getItemObjBySite")
	public @ResponseBody List<SiteItemBean> getItemObjBySite(@RequestParam("siteID") String siteID) {
		List<SiteItemBean> result = siteItemService.getSiteItemBySiteID(siteID);
		return result;
	}
	
	
	@RequestMapping(value="/addSiteItem",method=RequestMethod.POST)
	public @ResponseBody SiteItemBean addSiteItem(@RequestParam("siteID") String siteID, @RequestParam("itemID") String itemID,
			@RequestParam("eMaxValue") double eMaxValue, @RequestParam("eMinValue") double eMinValue, 
			@RequestParam("overMaxValue") double overMaxValue, @RequestParam("overMinValue") double overMinValue,
			@RequestParam("standUnit") String standUnit, @RequestParam("scale") int scale, 
			@RequestParam("maxRange") double maxRange, @RequestParam("minRange") double minRange,
			@RequestParam("factor") float factor, @RequestParam("alarmID") int alarmID, @RequestParam("remark") String remark) {
		SiteBean sBean = siteService.getSiteByID(siteID);
		ItemBean iBean = itemService.getItemByID(itemID);
		AlarmConfigBean acBean = alarmConfigService.getAlarmConfigByID(alarmID);
		return siteItemService.addSiteItem(sBean, iBean, eMaxValue, eMinValue, overMaxValue, overMinValue, standUnit, scale, maxRange, minRange, factor, acBean, remark);					
	}
	
	//根据siteID,批量添加item，site已有的item不加入
	@RequestMapping(value="/addAllSiteItem",method=RequestMethod.POST)
	public @ResponseBody int addAllSiteItem(@RequestParam("siteID") String siteID) {
		return siteItemService.addAllSiteItems(siteID);
	}
	
	@RequestMapping(value="/updateSiteItem")
	public @ResponseBody SiteItemBean updateSiteItem(@RequestParam("id") int id, @RequestParam("siteID") String siteID, @RequestParam("itemID") String itemID,
			@RequestParam("eMaxValue") double eMaxValue, @RequestParam("eMinValue") double eMinValue, 
			@RequestParam("overMaxValue") float overMaxValue, @RequestParam("overMinValue") double overMinValue,
			@RequestParam("standUnit") String standUnit, @RequestParam("scale") int scale, 
			@RequestParam("maxRange") double maxRange, @RequestParam("minRange") double minRange,
			@RequestParam("factor") float factor, @RequestParam("alarmID") int alarmID, @RequestParam("remark") String remark) {
		
		SiteBean sBean = siteService.getSiteByID(siteID);
		ItemBean iBean = itemService.getItemByID(itemID);
		AlarmConfigBean acBean = alarmConfigService.getAlarmConfigByID(alarmID);
		return siteItemService.updateSiteItem(id, sBean, iBean, eMaxValue, eMinValue, overMaxValue, overMinValue, standUnit, scale, maxRange, minRange, factor, acBean, remark);					
	}
	
	@RequestMapping(value="/getSiteItemByID")
	public @ResponseBody SiteItemBean getSiteItemByID(@RequestParam("id") int id) {
		SiteItemBean bean = siteItemService.getSiteItemByID(id);
		return bean;		
	}
	
	@RequestMapping(value="/deleteSiteItem")
	public @ResponseBody String deleteSiteItem(@RequestParam("eID") int id) {
		siteItemService.deleteSiteItem(id);
		return "success";
	}
}
